﻿using System;
using System.Data;
using System.Configuration;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Text;

/// <summary>
/// Base class for all multi part values.
/// </summary>
/// <typeparam name="T"></typeparam>
public abstract class BaseMultiPartValue<T>
{
	#region PUBLIC
	#region INIT
	/// <summary>
	/// Ctor.
	/// </summary>
	public BaseMultiPartValue()
	{
		Separators = new[] { ",", ";", "|", " " };
	}
	#endregion
	#region PROPS
	/// <summary>
	/// Array of strings acting as the separators.
	/// </summary>
	public string[] Separators { get; set; }
	/// <summary>
	/// Checks if any collection has been in use and if so, re-builds the string value from that collection.
	/// </summary>
	public virtual string Value
	{
		get
		{
			if (_valueList != null)
			{
				_value = GetValue(_valueList);
				NullifyLists();
			}
			return _value;
		}
		set
		{
			_value = value;
			NullifyLists();
		}
	}
	/// <summary>
	/// If this collection's last use was before the string value's last use, build the collection.
	/// </summary>
	public virtual List<T> ValueList
	{
		get
		{
			if (_valueList == null)
			{
				_valueList = new List<T>(GetValueList(Value)); // use the property here to ensure all other collections are nullifed.
			}
			return _valueList;
		}
	}
	#endregion
	#endregion
	#region INTERNAL
	#region VARS
	/// <summary>
	/// The concatenated string value.
	/// </summary>
	protected string _value;
	/// <summary>
	/// The collection of values.
	/// </summary>
	protected List<T> _valueList;
	#endregion
	#region METHODS
	/// <summary>
	/// Nullifies all lists when re-building the concatenated string.
	/// </summary>
	protected virtual void NullifyLists()
	{
		_valueList = null;
	}
	/// <summary>
	/// When implemented, splits the concatenated string into its parts.
	/// </summary>
	/// <param name="value"></param>
	/// <returns></returns>
	protected abstract T[] GetValueList(string value);
	/// <summary>
	/// When implemented, joins the value collection into a concatenated string.
	/// </summary>
	/// <param name="valueList"></param>
	/// <returns></returns>
	protected abstract string GetValue(List<T> valueList);
	#endregion
	#endregion
}
